﻿using MES.Server.Common;
using MES.Server.Entities;
using MES.Server.IService;
using MES.Server.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MES.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BaseInfoController : ControllerBase
    {
        IDepartService _departService;
        IPositionService _positionService;
        IFuncService _funcService;
        public BaseInfoController(
            IDepartService departService,
            IPositionService positionService,
            IFuncService funcService
            )
        {
            _departService = departService;
            _positionService = positionService;
            _funcService = funcService;
        }

        #region  部门

        [HttpGet("depart/{key}")]
        [Authorize]
        public async Task<JsonResult> GetDepartments([FromRoute] string key)
        {
            ApiDataResult<DepartEntity[]> result = new ApiDataResult<DepartEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                var ms = _departService.GetAllDepartment(key);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("depart/update")]
        [Authorize]
        public async Task<JsonResult> UpdateDepartment(DepartEntity depart)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _departService.Update(depart);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("depart/delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeleteDepartment(string[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _departService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("depart/check/{id}/{name}")]
        [Authorize]
        public async Task<JsonResult> CheckDepartName(string name, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _departService.CheckName(name, id);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        #endregion


        #region  职位

        [HttpGet("post/{key}")]
        [Authorize]
        public async Task<JsonResult> GetPositions([FromRoute] string key)
        {
            ApiDataResult<PostEntity[]> result = new ApiDataResult<PostEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                var ms = _positionService.GetAllPosition(key);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("post/update")]
        [Authorize]
        public async Task<JsonResult> UpdatePosition(PostEntity post)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _positionService.Update(post);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("post/delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeletePosition(string[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _positionService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("post/check/{id}/{name}")]
        [Authorize]
        public async Task<JsonResult> CheckPostName(string name, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _positionService.CheckName(name, id);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
        #endregion

        #region  功能

        [HttpGet("func/{key}")]
        [Authorize]
        public async Task<JsonResult> GetFunctions([FromRoute] string key)
        {
            ApiDataResult<FuncEntity[]> result = new ApiDataResult<FuncEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                var ms = _funcService.GetAllFunction(key);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("func/update")]
        [Authorize]
        public async Task<JsonResult> UpdateFunction(FuncEntity func)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _funcService.Update(func);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));     
        }

        [HttpPost("func/delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeleteFunction(string[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _funcService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("func/check/{id}/{name}")]
        [Authorize]
        public async Task<JsonResult> CheckFuncName(string name, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _funcService.CheckFuncName(name, id);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("func/ids/{ids}")]
        [Authorize]
        public async Task<JsonResult> GetFunctionByIds([FromRoute] string ids)
        {
            ApiDataResult<FuncEntity[]> result = new ApiDataResult<FuncEntity[]>();
            try
            {
                var ms = _funcService.GetFunctionByIds(ids);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
        #endregion
    }
}
